package com.huihui.adapter;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.huihui.R;
import com.huihui.base.BaseActivity;
import com.huihui.bean.MineZan;
import com.huihui.bean.Photo;
import com.huihui.bean.ZuJi;
import com.huihui.bean.ZuJiInfo;
import com.huihui.utils.ImageUtils;
import com.huihui.utils.IntentUtils;
import com.huihui.utils.LoadImageUtil;
import com.huihui.utils.StringUtils;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.List;


/**
 * Created by Administrator on 2018/8/18.
 * 我的点赞适配器
 */

public class ZujiAdapter extends RecyclerView.Adapter<MyRecycleHolder> {

    private Context context;
    private List<ZuJi> mList;
    private MineZanGridViewAdapter adapter;

    public ZujiAdapter(Context context, List<ZuJi> list) {
        this.context = context;
        this.mList = list;
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public MyRecycleHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.my_footer_item, parent, false);
        return new MyRecycleHolder(view);
    }


    @Override
    public void onBindViewHolder(MyRecycleHolder holder, int position) {
        final ZuJi zuJi = mList.get(position);
       final List<ZuJiInfo> data = zuJi.getList();
        TextView time = (TextView) holder.getView(R.id.zuji_time);
        time.setText(zuJi.getTime());
        ListView listView = (ListView) holder.getView(R.id.zuji_listview);
        MyAdapter adapter = new MyAdapter(context, R.layout.foot_listview_item, data);
        listView.setAdapter(adapter);
        setListViewHeight(listView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ZuJiInfo zuJiInfo = data.get(position);
                int class_id = zuJiInfo.getClass_id();
                if(class_id == 1){
                    long video_id = zuJiInfo.getZujiId();
                    IntentUtils.gotoSmallVideoInfoActivity(context,video_id);
                }else {

                    List<Photo> tempPhotoList = zuJiInfo.getTempPhotoList();
                    zuJiInfo.setPhotoList(tempPhotoList);
                    IntentUtils.gotoDetialAcitvity(context, zuJiInfo, position);
                }
            }
        });

    }

    class MyAdapter extends CommonAdapter<ZuJiInfo> {
        private Context context;

        public MyAdapter(Context context, int layoutId, List<ZuJiInfo> datas) {
            super(context, layoutId, datas);
            this.context = context;
        }

        @Override
        protected void convert(ViewHolder viewHolder, final ZuJiInfo item, int position) {
            LinearLayout itemLayout = viewHolder.getView(R.id.footer_item_layout);
            TextView name = viewHolder.getView(R.id.author_nickname);
            TextView content = viewHolder.getView(R.id.author_conent);
            ImageView icon = viewHolder.getView(R.id.author_headimg);
            name.setText(item.getName());
            content.setText(item.getContent());

            if(StringUtils.isGif(item.getIconPath())){
                LoadImageUtil.displayImage(context, item.getIconPath(), icon);
            }else {
                LoadImageUtil.displayJuxingImage(context, item.getIconPath(), icon);
            }
//            if(listener != null){
//                itemLayout.setTag(position);
//                itemLayout.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        int p = (int) v.getTag();
//                        List<Photo> tempPhotoList = item.getTempPhotoList();
//                        item.setPhotoList(tempPhotoList);
//                        IntentUtils.gotoDetialAcitvity(mContext,item,p);
//                    }
//                });
//            }
        }
    }

    public void setListViewHeight(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }
        if (listAdapter.getCount() == 0) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        int heights = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }
    public interface OnItemClickListener{
        void onClick(int position);
    }
    private  OnItemClickListener listener;
    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener  = listener;
    }

}
